<script setup lang="ts">
import XtxSwiperVue from '@/components/XtxSwiper.vue'
import CustomNavBar from './componets/CustomNavbar.vue'
import HotPanel from './componets/HotPanel.vue'
import { ref } from 'vue'
import type { BannerItem, CategoryItem, HotItem } from '@/types/home'
import {
  getHomeBannerAPI,
  getHomeCategoryAPI,
  getHomeHotAPI,
} from '@/services/home'
import { onLoad } from '@dcloudio/uni-app'
import CategoryPanel from './componets/CategoryPanel.vue'
import type { XtxGuessInstance } from '@/types/components'
import gip from './componets/gjp.vue'
//轮播图
const list = ref<BannerItem[]>([])
const getHomeBannerData = async () => {
  const res = await getHomeBannerAPI()
  // console.log(res)

  list.value = res.result
}
//分类
const cgplist = ref<CategoryItem[]>([])
const getCategoryData = async () => {
  const res = await getHomeCategoryAPI()
  // console.log(res)

  cgplist.value = res.result
}
//热门推荐
const hilist = ref<HotItem[]>([])
const getCategoryDat = async () => {
  const res = await getHomeHotAPI()
  // console.log(res)
  hilist.value = res.result
}
// 获取更多
const guess = ref<XtxGuessInstance>()
const scrollDown = () => {
  guess.value.getMore()
}
const isLoading = ref(false)
onLoad(async () => {
  isLoading.value = true
  await Promise.all([getHomeBannerData(), getCategoryData(), getCategoryDat()])
  isLoading.value = false
})
// 下拉刷新
const swp = ref()
const isrefresh = ref(false)
const onRefresh = async () => {
  isrefresh.value = true
  guess.value?.resetData()
  list.value = []
  swp.value.reset()
  await Promise.all([getHomeBannerData(), getCategoryData(), getCategoryDat()])
  isrefresh.value = false
}
</script>

<template>
  <custom-nav-bar></custom-nav-bar>
  <scroll-view
    scroll-y
    class="scroll-view"
    @scrolltolower="scrollDown"
    refresher-enabled
    :refresher-triggered="isrefresh"
    @refresherrefresh="onRefresh"
  >
    <gip v-if="isLoading"></gip>
    <template v-else>
      <XtxSwiperVue :list="list" ref="swp"></XtxSwiperVue>
      <CategoryPanel :list="cgplist" />
      <HotPanel :list="hilist" />
      <XtxGuess ref="guess" />
    </template>
  </scroll-view>
</template>

<style lang="scss">
page {
  background-color: #f7f7f7;
  height: 100%;
  display: flex;
  flex-direction: column;
}
.scroll-view {
  flex: 1;
}
</style>
